﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GiaSu.DataAccess.Entity;

namespace GiaSu.DataAccess.Repository
{
    public class GenericRepository<T> : IGenericRepository<T> where T : Base
    {
        private readonly DbContext dbContext;
        private readonly DbSet<T> dbSet;
        public GenericRepository(DbContext dbContext)
        {
            this.dbContext = dbContext;
            this.dbSet = dbContext.Set<T>();
        }

        public IQueryable<T> GetAll()
        {
            return dbSet;
        }

        public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return dbSet.Where(predicate);
        }

        public void Add(T entity)
        {
            this.dbSet.Add(entity);
        }

        public void Delete(T entity)
        {
            this.dbSet.Remove(entity);
        }

        public void Edit(T entity)
        {
            this.dbContext.Entry(entity).State = EntityState.Modified;
        }

        public void SaveChanges()
        {
            this.dbContext.SaveChanges();
        }
    }
}
